<?php

/**
 * 抽象原型
 */
interface Prototype {  
    public function copy();  
}  

/**
 * 具体原型
 */
class People implements Prototype
{
	private $_name;
	private $_age;
	private $_address;
	
	public function __construct($name, $age, $address)
	{
		$this->_name = $name;
		$this->_age = $age;
		$this->_address = $address;
	}
	
	public function setName($name)
	{
		$this->_name = $name;
	}
	
	public function getName()
	{
		return $this->_name;
	}
	
	public function setAge($age)
	{
		$this->_age = $age;
	}
	
	public function getAge()
	{
		return $this->_age;
	}
	
	public function setAddress(Address $address)
	{
		$this->_address = $address;
	}
	
	public function getAddress()
	{
		return $this->_address;
	}
	
	public function copy()
	{
		//深拷贝
		//return unserialize(serialize($this));
		//浅拷贝
		return clone $this;
	}
}

class Address
{
	private $_city;
	private $_street;
	
	public function __construct($city, $street)
	{
		$this->_city = $city;
		$this->_street = $street;
	}
	
	public function getCity()
	{
		return $this->_city;
	}
	
	public function getStreet()
	{
		return $this->_street;
	}
}

class Client
{
	public static function main()
	{
		//原型
		$address = new Address('深圳', '宝安区樟坑三区');
		$people = new People('粟刚', 24, $address);
		
		echo '原型对象<br/>';
		echo "名称：{$people->getName()}<br/>";
		echo "年龄：{$people->getAge()}<br/>";
		echo "地址：{$people->getAddress()->getCity()}<br/>";
		//复制
		$people2 = $people->copy();
		echo '克隆对象<br/>';
		echo "名称：{$people2->getName()}<br/>";
		echo "年龄：{$people2->getAge()}<br/>";
		echo "地址：{$people2->getAddress()->getCity()}<br/>";
		
		//判断
		echo '对象是否相同：' . ($people === $people2 ? 'yes' : 'no') . '<br/>';
		echo '对象的地址是否相同：' . ($people->getAddress() === $people2->getAddress() ? 'yes' : 'no') . '<br/>';
	}
}

Client::main();